package com.dmm.redis.service.impl;

import com.alibaba.fastjson.JSON;
import com.dmm.redis.service.redisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
public class redisServiceImpl<T> implements redisService<T> {

    @Resource
    RedisTemplate<String,String> redisTemplate;

    //添加
    @Override
    public void addset(String key, String value) {

        redisTemplate.opsForValue().set(key, JSON.toJSONString(value));

    }

    //添加
    @Override
    public void set2(String key, String value, Integer i) {
        redisTemplate.opsForValue().set(key,JSON.toJSONString(value),i, TimeUnit.MINUTES);
    }

    @Override
    //添加,过期时间,单位分
    public void set(String key,List<T> value,Integer i){

        redisTemplate.opsForValue().set(key,JSON.toJSONString(value),i, TimeUnit.MINUTES);


    }

    //获取建值
    @Override
    public List<?> get(String key, Class<T> clazz) {

        String s = redisTemplate.opsForValue().get(key);
        List<?> objects = JSON.parseArray(s,clazz);

        return objects;
    }

    @Override
    public String get(String key) {

        return redisTemplate.opsForValue().get(key);
    }


    //删除指定键
    @Override
    public Boolean delete(String key) {

        Boolean delete = redisTemplate.delete(key);
        return delete;
    }

    @Override
    //查询建是否存在
    public Boolean getKey(String key){

        Set<String> keys = redisTemplate.keys(key);

        if(keys.isEmpty()){
            return false;
        }
        return true;
    }
}
